package com.lul15834.orderfood.activity.manager.adapter;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.lul15834.orderfood.R;
import com.lul15834.orderfood.bean.OrderDetailBean;

import java.math.BigDecimal;
import java.util.List;

/**
 * 显示商家商品的adapter
 */
public class OrderNoDetailAdapter extends RecyclerView.Adapter<OrderNoDetailAdapter.OrderViewHolder> {
    private List<OrderDetailBean> list;
    public OrderNoDetailAdapter(@NonNull Context context, List<OrderDetailBean> list){
            this.list=list;
    }

    /**
     * 获取商品总计
     *
     * @return
     */
    public String getSumPrice(){
        //所有商品数量*价格
        BigDecimal total=new BigDecimal(0);
        for (OrderDetailBean orderDetailBean:list){
            BigDecimal Price=new BigDecimal(orderDetailBean.getFoodPrice());
            BigDecimal Num=new BigDecimal(orderDetailBean.getFoodQuantity());
            BigDecimal dj=Price.multiply(Num);//价格是总价 dj=Price*Num
            total=total.add(dj);
        }
        return  total.toString();
    }
    @NonNull
    @Override
    public OrderNoDetailAdapter.OrderViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater=LayoutInflater.from(parent.getContext());
        View convertView=inflater.inflate(R.layout.food_no_detail_list,parent,false);
        return new OrderViewHolder(convertView);
    }

    @Override
    public void onBindViewHolder(@NonNull OrderNoDetailAdapter.OrderViewHolder holder, int position) {
        OrderDetailBean tem=list.get(position);
        holder.imageView.setImageBitmap(BitmapFactory.decodeFile(tem.getFoodImg()));
        holder.name.setText(tem.getFoodName());
        holder.num.setText(tem.getFoodQuantity()+"份");
        BigDecimal Price=new BigDecimal(tem.getFoodPrice());
        BigDecimal Num=new BigDecimal(tem.getFoodQuantity());
        BigDecimal total=Price.multiply(Num);
        holder.price.setText("￥ "+total.toString());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
    static class OrderViewHolder extends RecyclerView.ViewHolder{
        ImageView imageView;
        TextView name;
        TextView num;
        TextView price;
        public OrderViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView=itemView.findViewById(R.id.list_order_no_list_img);
            name=itemView.findViewById(R.id.list_order_no_list_name);
            num=itemView.findViewById(R.id.list_order_no_list_num);
            price=itemView.findViewById(R.id.list_order_no_list_price);
        }
    }


}

